import axios from 'axios';

export const fetchUsers = () => async (dispatch) => {
    try {
        const reasponse = await axios.get('http://localhost:8000/users');
        dispatch({ type: 'SET_USERS', payload: reasponse.data })
        dispatch({ type: 'SET_MESSAGE', payload: 'Users loaded!' })
    } catch (error) {
        console.log(error);
    }
}

export const fetchTasks = () => async (dispatch) => {
    try {
        const response = await axios.get('http://localhost:8000/tasks');
        dispatch({ type: 'SET_TASKS', payload: response.data });
    } catch (error) {
        console.log(error);
    }
}

export const addTask = (task) => async (dispatch) => {
    try {
        const response = await axios.post('http://localhost:8000/tasks', task);
        dispatch({ type: 'ADD_TASK', payload: response.data });
    } catch (error) {
        console.error(error);
    }
};

export const login = (username) => (dispatch) => {
    dispatch({ type: 'SET_CURRENT_USER', payload: { username } });
}